<!DOCTYPE html>
<html>
    <?php

    include_once "../DB/db.php";
    include_once "../Functions/imageFunctions.php";
    include_once "../class/ItemClass.php";

    ?>
    <head>
        <title>Nettbutikk</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../CSS/mainFrame.css" />
        <link rel="stylesheet" type="text/css" href="../CSS/content.css" />
    </head>
    <body>
        <div class="wrapper"> 
            <?php
                include_once "../MainFrame/header.php";
                include_once "../MainFrame/menuBar.php";
                include_once "../MainFrame/sideBar.php";
            ?>

            <div class="content">
                <?php
                // Show admin choices if user is admin and is logged on:
                    if($user->isLoggedOn() && $user->isAdmin()){
                        showAdminItem(); // show admin choices
                    }?>

                <?php
                // Get neccecary itemdata:
                $my_item = $_GET['itemId']; 
                $itemQuery = $item->getAllItemData($my_item);
                ?>

                <?php
                // Process and get the neccecary data:
                $itemInfo = $itemQuery->fetch();
                $my_item_copy = ucwords($itemInfo['name']);
                
                $item_name = $itemInfo['name'];
                $item_price = $itemInfo['price'];
                $item_description = $itemInfo['description'];
                $item_stock = $itemInfo['stock'];
                $item_picture = $itemInfo['picture'];
                $item_deleted = $itemInfo['deleted'];
                $item_pi = getImageFromBlob($item_picture, 0, 0);
                ?>

                
                <h1 class="contentHeader"><?php echo $my_item_copy; ?></h1>
                <table class="item_table">
                    <tr>
                        <td>
                            <?php
                            echo $item_pi;
                            ?>
                        </td>
                        <!-- If the item is not deleted: -->
                        <?php if($item_deleted==0){?>
                        <td>
                            <p id="itemPrice">Nå <span id="price"><?php echo $item_price?>,-</span></p>
                             <form class="buyItem" method="GET" action="/Functions/cookieFunctions.php">
                                <input name="antall" type="text" value="1" class="antall">
                                <input name="vareNavn" type="hidden" value="<?php echo $my_item ?>">
                                <input name="vareKjop" type="submit" value="Kjop">
                             </form>
                            <p> Lagerstatus: <?php echo $item_stock ?> på lager. <br>
                            </p>
                        </td>
                        <?php } else { ?>
                        <td>Beklager. Denne varen er ikke lenger støttet.<br>
                        Hvis du har bestillinger av denne varen, får du varen uten problem.</td>
                        <?php }
                        ?>
                    </tr>
                </table>

                <!-- Show advanced options: To be continued-->
                <table id="item_description">
                    <tr><th>Full beskrivelse                </th></tr>
                    <tr><td><?php echo $item_description; ?></td></tr>
                    <tr><th>Reviews                         </th></tr>
                    <tr><td>To be continued...              </td></tr>
                    <tr><th>Avansert info                   </th></tr>
                    <tr><td>To be continued...              </td></tr>
                </table>

            </div><br>

            <?php include_once "../MainFrame/footer.php"; ?>
        </div>
    </body>


    <?php

    // Show admin choices Change and Delete item.
    function showAdminItem(){
        ?>
        <div class="admin_item_table">
            <table>
                <tr>
                    <?php $this_item = $_GET['itemId']; ?>
                    <td><a href="/adminViews/adminItem.php?alterItem=alterItem&itemId=<?php echo $this_item;?>">Endre Vare</a></td>
                    <td><a href="/adminViews/adminItem.php?alterItem=deleteItem&itemId=<?php echo $this_item;?>">Slett Vare</a></td>
                </tr>
            </table>
        </div>
    <?php } ?>

    
</html>